package cn.itsource.basic.controller;

import cn.itsource.basic.dto.SmsDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IVerifyCodeService;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败");
        }
    }


    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.smsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}
